﻿using SignalProcessLib.Model.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SignalProcessLib
{
    public class DigitalSignalProcessing
    {
        public static float[] OverlapSignal(List<float[]> timewaves, float overlap)
        {
            if (overlap < 0 || overlap > 100)
                throw new Exception("overlap小于0，或大于100");

            int len = timewaves[0].Length;
            for (int i = 1; i < timewaves.Count; i++)
            {
                if (len != timewaves[i].Length)
                    throw new Exception("输入波形的长度不一致");
            }
            int opNum = Convert.ToInt32(len * overlap);
            List<float> result = new List<float>();
            for (int i = 0; i < timewaves.Count; i++)
            {

                if (i == 0)
                    result.AddRange(timewaves[i]);
                else
                    result.AddRange(timewaves[i].Skip(opNum).Take(len - opNum));
            }
            return result.ToArray();
        }

        public static float[] AverageAll(List<float[]> specs, AverageType averageType, float exponential = 0.5f)
        {
            int len = specs[0].Length;
            for (int i = 1; i < specs.Count; i++)
            {
                if (len != specs[i].Length)
                    throw new Exception("输入波形的长度不一致");
            }

            float[] result = new float[len];
            int count = specs.Count;
            switch (averageType)
            {
                case AverageType.Linear:

                    for (int i = 0; i < len; i++)
                    {
                        float temp = 0;
                        for (int j = 0; j < count; j++)
                        {
                            temp += specs[j][i];
                        }
                        result[i] = temp / Convert.ToSingle(count);
                    }
                    break;

                default:
                    break;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Xn">当前的频谱数据</param>
        /// <param name="Xnm1">上一次平均的结果</param>
        /// <param name="n">当前的采样次数</param>
        /// <param name="averageType">平均方式</param>
        /// <param name="exponential">指数平均的参数</param>
        /// <param name="peakHoldChannel">峰值保持</param>
        /// <returns></returns>
        public static float[] Average(float[] Xn, float[] Xnm1, float n, AverageType averageType, float exponential = 0.5f)
        {
            if (Xn.Length != Xnm1.Length)
                throw new Exception("输入波形的长度不一致");
            if (n == 0)
                throw new Exception("n必须为大于0的整数");


            int len = Xn.Length;
            float[] result = new float[len];
            switch (averageType)
            {
                case AverageType.Linear:
                    for (int i = 0; i < len; i++)
                    {
                        result[i] = (n - 1) / n * Xnm1[i] + Xn[i] / n;
                    }
                    break;

                default:
                    break;
            }

            return result;
        }
    }
}
